Shell脚本实战28-Nginx负载节点状态检测

开发通过Web界面展示监控Nginx代理节点的状态。当节点宕机时,以红色展示,当节点正常时以绿色展示。

这个脚本要求对Nginx服务很熟练,由于我的水平不够,所以仅以摘抄的方式收录了这个脚本。

Shell脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
RIPS=(
10.0.0.7
10.0.0.8
)
file_location=/var/html/test.html
[ -e "file_location" ] || mkdir `dirname $file_location` -p
function web_result()
{
rs=`curl -I -s $1 | awk 'NR==1{print $2}'`
return $rs
}
function new_row()
{
cat >> $file_location <<eof
<tr>
<td bgcolor="$4">$1</td>
<td bgcolor="$4">$2</td>
<td bgcolor="$4">$3</td>
</tr>
eof
}
function auto_html()
{
web_result $2
rs=$?
if [ $rs -eq 200 ]
then
new_row $1 $2 up green
else
new_row $1 $2 down red
fi
}
function main()
{
while true
do
cat >> $file_location <<eof
<h4>Nginx Server Status Of RS :</h4>
<meta http-equiv="refresh" content="1">
<table border="1">
<tr>
<th>NO:</th>
<th>IP:</th>
<th>Status:</th>
</tr>
eof
for ((i=0; i<${#RIPS[*]}; i++))
do
auto_html $i ${RIPS[$i]}
done
cat >> $file_location <<eof
</table>
eof
sleep 2
> $file_location
done
}
main $*

0%